import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                component: () =>
                    import ('../views/home/welcome.vue')
            },
            {
                path: '/rights',
                component: () =>
                    import ('../views/home/rights.vue')
            },
            {
                path: '/roles',
                component: () =>
                    import ('../views/home/roles.vue')
            },
            {
                path: '/users',
                component: () =>
                    import ('../views/home/users.vue')
            },
            {
                path: '/categories',
                component: () =>
                    import ('../views/home/categories.vue')
            },
            {
                path: '/params',
                component: () =>
                    import ('../views/home/params.vue')
            },
            {
                path: '/goods',
                component: () =>
                    import ('../views/home/goods.vue')
            },
        ]
    },
    {
        path: '/about',
        component: () =>
            import ('../views/About.vue')
    },
    {
        path: '/login',
        component: () =>
            import ('../views/login.vue')
    },
    {
        path: '*',
        redirect: '/'
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next()
        return
    }
    if (localStorage.getItem('token')) {
        next()
    } else {
        next('/login')
    }
})

export default router